package me.raih.controller;

import me.raih.service.SalesService;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import me.raih.util.MathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;
import java.util.Properties;

/**
 * @Author Raih
 * @Time 2020/11/27  21:02
 *
 * 欢迎页面
 */
@Controller
public class HomeController {

    @Autowired
    UserService userService;
    @Autowired
    SalesService salesService;
    @Autowired
    WaresService waresService;

    @RequestMapping(value = "/admin/welcome")
    public String toAdminHome(Model model){
        int WAM = waresService.findWaresNumber();
        int WTWN = waresService.findTodayWaresNumber();
        int WYMN = waresService.findYesterdayWaresNumber();
        int WCMN = waresService.findCurrMonthWaresNumber();
        int WCWN = waresService.findCurrWeekWaresNumber();
        model.addAttribute("WAM",WAM);
        model.addAttribute("WTWN",WTWN);
        model.addAttribute("WYMN",WYMN);
        model.addAttribute("WCMN",WCMN);
        model.addAttribute("WCWN",WCWN);

        int UAM = userService.findUserNumber();
        int UTUN = userService.findTodayUserNumber();
        int UYWN = userService.findYesterdayUserNumber();
        int UCMN = userService.findCurrMonthUserNumber();
        int UCWN = userService.findCurrWeekUserNumber();
        model.addAttribute("UAM",UAM);
        model.addAttribute("UTUN",UTUN);
        model.addAttribute("UYWN",UYWN);
        model.addAttribute("UCMN",UCMN);
        model.addAttribute("UCWN",UCWN);

        int SAM = salesService.findSalesNumber();
        int SWSN = salesService.findTodaySalesNumber();
        int SYSN = salesService.findYesterdaySalesNumber();
        int SCMN = salesService.findCurrMonthSalesNumber();
        int SCWN = salesService.findCurrWeekSalesNumber();
        model.addAttribute("SAM",SAM);
        model.addAttribute("SWSN",SWSN);
        model.addAttribute("SYSN",SYSN);
        model.addAttribute("SCWN",SCWN);
        model.addAttribute("SCWN",SCWN);


        //获取服务器信息
        Properties properties = System.getProperties();
        //String _NAME = properties.getProperty("USERNAME");
        String _ARCH = properties.getProperty("os.arch");
        String _OS = properties.getProperty("os.name");
        model.addAttribute("_ARCH",_ARCH);
        model.addAttribute("_OS",_OS);

        Map<String, String> getenv = System.getenv();
        String _NAME = getenv.get("COMPUTERNAME");
        model.addAttribute("_NAME",_NAME);

        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        double _Memory = MathUtil.twoDoubleNumber(((double)freeMemory*100)/(double)totalMemory);
        model.addAttribute("_Memory",_Memory);
        int _CORE = runtime.availableProcessors();
        model.addAttribute("_CORE",_CORE);

        return "admin/welcome";
    }

    @RequestMapping(value = "/sales/welcome")
    public String toSalesHome(){

        return "sales/welcome";
    }

}
